<?php
/**
 * @developer socheat
 * @copyright 2012
 */
require_once('../libraryfiles/config.php');
require_once TMP_USER_PATH;

if (isset($_GET['product'])) {
    $pro = $_GET['product'];
} else {
    echo 'have no product select';
}


$product_detail = new prodcut_tb();
$GetProduct = $product_detail->product_detail($pro);
$Get_rows = mysql_fetch_assoc($GetProduct);

//for company
$companys = new company();
$last_word = '';
$companys->getcompany($last_word, $Get_rows['owner']);

$span_left = 'span3';
$span_right = 'span9';

$fname = isset($_POST['fname']) ? $_POST['fname'] : '';
$lname = isset($_POST['lname']) ? $_POST['lname'] : '';
$email = isset($_POST['to_user_email']) ? $_POST['to_user_email'] : '';
if (!empty($_GET['post'])) {
    if ($_GET['post'] == 'add') {
        $subjects = $_POST['subject'];
        date_default_timezone_set('UTC');
        $date = date('d/m/yy/h:i:s');
        if (!empty($_GET['reply'])) {
            $reply = ',reply';
            $reply_valua = ',1';
            $idemail = isset($_GET['idemail']) ? $_GET['idemail'] : "";
            $reply_update = mysql_query("UPDATE contact SET reply=reply+1 WHERE email_id=$idemail");
        } else {
            $reply = '';
            $reply_valua = '';
        }
        $help = "INSERT INTO contact (userid,to_user,pro_id,quantity,title,message,added_date) VALUES ('" . $_POST['user_email'] . "','" . $_POST['to_user_email'] . "','" . $_POST['pro_id'] . "', '" . $_POST['quantity'] . "', '" . $_POST['subject'] . "', '" . $_POST['comment'] . "', '$date')";
        $contact_in = mysql_query($help);

        // for email
        // multiple recipients
        $to = 'sales@gmail.com'; //. ', '; // note the comma
        //$to .= 'socheat.ngann@mapring.com';
        // subject
        $subject = $_POST['subject'];

        // message
        $BodyMessage = '
        <html>
        <head>
          <title>' . $_POST['subject'] . '</title>
        </head>
        <body>
          <strong>Subject</strong> :' . $_POST['subject'] . '</p>
          <b>from</b> :' . $_POST['user_email'] . '<br/>
          <strong>Comment</strong> :' . $_POST['comment'] . '<br/>
          <table width="100%" border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td><strong>ID</strong></td>
                <td><strong>Img</strong></td>
                <td><strong>Product Name</strong></td>
                <td><strong>Date</strong></td>
              </tr>
              <tr>
                <td>' . $Get_rows['id'] . '</td>
                <td><img height="50" width="50" style="float: left;" class="img-active" src="' . W_S_IMAGES_DIR_THUMB . $Get_rows['images'] . '" /></td>
                <td><a href="' . base_url . 'product/product_detail.php?product_id=' . $Get_rows['id'] . '" title="' . $Get_rows['product_name'] . '" target="_blank">' . $Get_rows['product_name'] . '</a></td>
                <td>' . $date . '</td>
              </tr>
            </table>
        </body>
        </html>
        ';

        // To send HTML mail, the Content-type header must be set
        $headers = 'MIME-Version: 1.0' . "\r\n";
        $headers .= 'Content-type: text/html; charset=utf-8' . "\r\n";

        // Additional headers
        $headers .= 'To: Mary <mary@example.com>' . "\r\n";
        $headers .= 'From: Buyer Conact <birthday@example.com>' . "\r\n";
        //$headers .= 'Cc: birthdayarchive@example.com' . "\r\n";
        //$headers .= 'Bcc: birthdaycheck@example.com' . "\r\n";
        // Mail it
        //mail($to, $subject, $message, $headers);




        /* send email to confirm */
        include_once S_ROOT . '/libraryfiles/SendEmail.php';
        $transport = new SendEmail ();
        $name = $fname . $lname;
        $header = 'info.neakporn@gmail.com';
        $subjects = "Psarinternet Activation";
        $base = "http://" . $_SERVER ["SERVER_NAME"];
        $BodyHeader = "CUSTOMER CONTACT";
        //$BodyMessage = 'Your account is pending, please wait your activation by psarinternet administrator!';
        $transport = $transport->SendMail($name, $email, $base, $header, $subjects, $BodyHeader, $BodyMessage);
        /* end send email to confirm */
        // end for email          

        if ($transport) {
            echo "<p>Your request has been sent!</p>";
			?>
            <hr/>
			<a style="float:right;" class="button" href="<?php echo base_url;?>">Back home</a>
            <?php
        } else {
            echo "<p>Your request can not be send. please try again!</p>";
			?>
            <hr/>
			<a style="float:right;" class="button" href="<?php echo base_url;?>">Back home</a>
            <?php
        }
    }
} else {

    if (!empty($_GET['reply'])) {
        $reply = '&reply=1';
        $idemail = isset($_GET['idemail']) ? '&amp;idemail=' . $_GET['idemail'] : "";
    } else {
        $reply = '';
        $idemail = '';
    }
    ?>
    <style>
        #mce_0_toolbar2,#mce_0_toolbar3
        {
            display:none;
        }
    </style>
    
    <form method="post" id="contact" action="<?php echo W_ROOT; ?>/product/contact.php?product=<?php echo $Get_rows['id'] ?>&post=add<?php echo $reply . $idemail; ?>">
        <!-- Main Page Content -->

        <div class="row-fluid">
            <!-- Contact Details -->
            <div class="span12 category">
                <div id="content">
                    <div class="title" style="margin-bottom: 15px;">
                        <h1 class="category-title"><?php echo $lang['CON_SUPPLIER']; ?></h1>                    
                    </div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline"><!--Your Email--><?php echo $lang['SHIP_FIR_NAME']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>"><input type="text" name="fname" id="user_fname" value="" /></div>
                    </div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline"><!--Your Email--><?php echo $lang['SHIP_LAS_NAME']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>"><input type="text" name="lname" id="user_fname" value="" /></div>
                    </div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline"><!--Your Email--><?php echo $lang['YOUREMAIL']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>"><input type="text" name="user_email" id="user_email" placeholder="jane@khm.com" value="<?php
                            if (!empty($_SESSION['username'])) {
                                echo $_SESSION['username'];
                            }
                            ?>" /></div>
                    </div>
                    <div class="clear"></div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline"><!--To--><?php echo $lang['TO']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>"><input type="text" name="to_user_email" id="to_user_email" placeholder="jane@khm.com" value="<?php
                            if (!empty($_GET['reply'])) {
                                echo $_GET['reply'];
                            } else {
                                echo $companys->email;
                            }
                            ?>" /></div>
                    </div>
                    <div class="clear"></div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline"><!--Product--><?php echo $lang['PRODUCT']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>">                
                            <img height="50" width="50" style="float: left;margin-right: 5px;" class="img-active" src="<?php echo W_S_IMAGES_DIR_THUMB . $Get_rows['images']; ?>" /><a href="<?php echo W_ROOT; ?>/product/product_detail.php?product_id=<?php echo $Get_rows['id'] ?>" title="<?php echo $Get_rows['product_name']; ?>" target="_blank"><?php echo $Get_rows['product_name']; ?></a><br /><span class="ddd" style="color:#888; font-size: 90%;"><?php
                                echo $companys->pharmacy_name;
                                echo ' ,' . $companys->address;
                                ?></span>
                        </div>
                    </div>
                    <div class="clear" style="margin-bottom: 10px;"></div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline">* <!--Subject--><?php echo $lang['SUBJECTT']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>"><input type="text" name="subject" id="subject" placeholder="fill your subject here" /></div>
                    </div>
                    <div class="clear"></div>
                    <div class="row-fluid">
                        <div class="<?php echo $span_left; ?>"><label class="inline"><!--Quantity Needed--><?php echo $lang['QUANTITYNEEDE']; ?>:</label></div>
                        <div class="<?php echo $span_right; ?>"><input style="width: 40px; float:left; text-align: center;" type="number" name="quantity" id="quantity" value="" /></div>
                    </div>



                    <label><!--Comment--><?php echo $lang['COMMENT']; ?></label>
                    <textarea rows="4" name="comment"></textarea>
                    <input name="pro_id" type="hidden" value="<?php echo $Get_rows['id'] ?>" />
                    <div class="row-fluid" style="margin-top: 10px;">
                    <div class="span12">
                        <input type="submit"
                               value="<?php echo $lang['SUBMIT']; ?>"
                               class="button pull-right" name="contact_to" />
                    </div>
                    <!-- End Contact Details -->  
            </div>
                </div>
        </div>
        <!-- End Main Content -->
    </form>  
    <script type="text/javascript" src="<?php echo base_url; ?>admin/templates/javascript/ckeditor/ckeditor.js"></script>
    <script type="text/javascript" src="<?php echo base_url; ?>templates/javascripts/tabs.js"></script>
    <script>
        $(document).ready(function() {
            CKEDITOR.replace('comment', {
                toolbar: [
                    {name: 'tools', items: ['Maximize']},
                    {name: 'basicstyles', groups: ['basicstyles', 'cleanup'], items: ['Bold', 'Italic', 'Strike', '-', 'RemoveFormat']},
                    {name: 'links', items: ['Link', 'Unlink', 'Anchor']},
                    {name: 'paragraph', groups: ['list', 'indent', 'blocks', 'align', 'bidi'], items: ['NumberedList', 'BulletedList', '-', 'Outdent', 'Indent', '-', 'Blockquote']},
                    {name: 'document', items: ['Source', '-', 'NewPage', 'Preview', '-', 'Templates']},
                ]
            });
            
            /*validate*/
            $('#contact').validate({
                rules: {
                    accounttype: {
                        required: true
                    },
                    passwd: {
                        required: true,
                        minlength: 6
                    },
                    email_add: {
                        required: true,
                        email: true
                    },
                },
                messages: {
                    passwd: {
                        required: "<?php echo $lang['INPUTPASSWORD']; ?>",
                        minlength: "<?php echo $lang['INPUT_PASSWORD_MIN']; ?>"
                    },
                    email_add: "<?php echo $lang['EMAILSMSS']; ?>",
                }
            });
            /*end validate*/
        });
    </script>
    <?php
//date_default_timezone_set('UTC');
//date('d/m/yy/h:i:s');
}
?>

